﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;

using TrainingCenterSystem;
using TrainingCenterSystem.VocabularyManagement;
using TrainingCenterSystem.UserManagement;

namespace TrainingCenterSystem.TestManagement
{
    public class TestGenerator
    {
        #region private members
        private VocabularyTraining _vocTraining;
        private List<GenericWord> _sources;
        private CollectionHandler _collectionHandler;

        #endregion

        #region setters/getters
        /// <summary>
        /// Gets the training session created by the generator
        /// </summary>
        public VocabularyTraining VocTraining
        {
            get { return _vocTraining; }
        }

        /// <summary>
        /// Gets the selected sources for the test/training
        /// </summary>
        public List<GenericWord> Sources
        {
            get { return _sources; }
        }
        #endregion

        #region constructor
        /// <summary>
        /// Creates a test generator object
        /// </summary>
        /// <param name="testName">the name of the test</param>
        /// <param name="testDescription">the description of the test</param>
        /// <param name="noOfQuestions">the number of questions</param>
        /// <param name="level">the test level</param>
        /// <param name="type">the test type</param>
        /// <param name="direction">the test direction</param>
        /// <param name="selSources">the selected sources</param>
        public TestGenerator(string testName, string testDescription, byte noOfQuestions, TestLevel level, TestType type, TestDirection direction,ArrayList selSources, CollectionHandler collectionHandler)
        {
            _vocTraining = new VocabularyTraining(testName, testDescription, noOfQuestions, level, type, selSources, direction);            
            _collectionHandler = collectionHandler;

            //this.transformSources();

            //this.generateQuestions();
        }
        #endregion

        #region methods
        public void TransformSources()
        {
            _sources = new List<GenericWord>();
            for (int i = 0; i < _vocTraining.SelectedSources.Count; i++)
            {
                if (_vocTraining.SelectedSources[i] is VocabularyCollection)
                {
                    VocabularyCollection aColl = (VocabularyCollection)_vocTraining.SelectedSources[i];
                    for (int j = 0; j < aColl.Words.Count; j++)                    
                        _sources.Add((GenericWord)aColl.Words[j]);                    
                }
                else if (_vocTraining.SelectedSources[i] is VocabularyContext)
                {
                    VocabularyContext aCont = (VocabularyContext)_vocTraining.SelectedSources[i];
                    VocabularyCollection aColl=_collectionHandler.GetCollectionByFileName(aCont.CollectionID);
                    if (aColl!= null)
                    {
                        List<GenericWord> wordList = aColl.GetAllFromContext(aCont.ContextName);
                        for (int j = 0; j < wordList.Count; j++)                        
                            _sources.Add((GenericWord)wordList[j]);                                            
                    }
                    else
                    {
                    }
                }
            }
        }
        /*private void generateTest()
        {
            //_sources.RemoveAt
        }*/

        public void GenerateQuestions()
        {
            for (int i = 0; i < _vocTraining.NumberOfQuestions; i++)
            {                                  
                // which type of question?
                if (_vocTraining.TestType == TestType.FreeText)
                {
                    FreeTextQuestion freeText = new FreeTextQuestion(i.ToString(), _vocTraining);
                    this.generateFreeTextQuestion(freeText);
                }
                else if (_vocTraining.TestType == TestType.MultipleChoice) // not available in the current version, will be fixed in the next release
                {
                }
                else // not available in the current version, will be fixed in the next release
                {
                }                
            }
        }

        private void generateFreeTextQuestion(FreeTextQuestion aQuestion)
        {
            // choose the word of this particular question
            Random random = new Random();
            int iWordIndex = random.Next(_sources.Count);
            GenericWord aWord = _sources[iWordIndex];
            _sources.RemoveAt(iWordIndex);
            
            /* which level? (basic or extended?)
               not available in the current version, will be fixed in the next release
            if (_vocTraining.TestLevel == TestLevel.Basic)
            {
                
            }
            else if (_vocTraining.TestLevel == TestLevel.Extended) 
            {
            }*/

            // which direction? ()
            if (_vocTraining.TestDirection == TestDirection.TranslationToBase)
            {
                aQuestion.TheWord=aWord;
                aQuestion.WordString = aWord.Translations;
                aQuestion.ExpectedAnswer=aWord.BaseValue;
            }
            else if (_vocTraining.TestDirection == TestDirection.BaseToTranslation)
            {
                aQuestion.TheWord = aWord;
                aQuestion.WordString = aWord.BaseValue;
                aQuestion.ExpectedAnswer = aWord.Translations;
            }
            else
            {
                if (this.determineTestDirection() == TestDirection.TranslationToBase)
                {
                    aQuestion.TheWord = aWord;
                    aQuestion.WordString = aWord.Translations;
                    aQuestion.ExpectedAnswer = aWord.BaseValue;
                }
                else if (this.determineTestDirection() == TestDirection.BaseToTranslation)
                {
                    aQuestion.TheWord = aWord;
                    aQuestion.WordString = aWord.BaseValue;
                    aQuestion.ExpectedAnswer = aWord.Translations;
                }
            }

            _vocTraining.TestQuestions.Add(aQuestion);
        }

        /// <summary>
        /// Randomly determines the test level
        /// </summary>
        /// <returns>the test level</returns>
        private TestLevel determineTestLevel()
        {
            return TestLevel.Basic;
        }

        /// <summary>
        /// Randomly determines the test type
        /// </summary>
        /// <returns>the test type</returns>
        private TestType detrmineTestType()
        {
            return TestType.FreeText;
        }

        /// <summary>
        /// Randomly determines the test direction
        /// </summary>
        /// <returns>the test direction</returns>
        private TestDirection determineTestDirection()
        {
            Random random = new Random();
            int iDirection = random.Next(0, 2);
            if (iDirection == 0)
                return TestDirection.TranslationToBase;
            else
                return TestDirection.BaseToTranslation;            
        }
        #endregion
    }
}
